<template>
  <div class="crv-form" :style="formStyle">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth" :model="modelValue" ref="ruleFormRef">
      <el-row>
        <template v-for="item in formItems" :key="item">
          <el-col v-bind="colLayout">
            <el-form-item
              :label="item.lable"
              :rules="item.rules"
              :style="itemStyle"
              :prop="item.field"
              v-if="!item.isHidden"
            >
              <template v-if="item.type === 'password'">
                <el-input
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <template #prepend v-if="item.showPre">
                    <slot :name="`${item.field}_prepend`" :row="item"></slot>
                  </template>
                  <template #append v-if="item.showSub">
                    <slot :name="`${item.field}_append`" :row="item"></slot>
                  </template>
                </el-input>
              </template>

              <template v-else-if="item.type === 'textarea'">
                <el-input
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <template #prepend v-if="item.showPre">
                    <slot :name="`${item.field}_prepend`" :row="item"></slot>
                  </template>
                  <template #append v-if="item.showSub">
                    <slot :name="`${item.field}_append`" :row="item"></slot>
                  </template>
                </el-input>
              </template>

              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  clearable
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.title }}</el-option
                  >
                </el-select>
              </template>

              <template v-else-if="item.type === 'datePicker'">
                <el-date-picker
                  clearable
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                ></el-date-picker>
              </template>
              <template v-else>
                <el-input
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <template #prepend v-if="item.showPre">
                    <slot :name="`${item.field}_prepend`" :row="item"></slot>
                  </template>
                  <template #append v-if="item.showSub">
                    <slot :name="`${item.field}_append`" :row="item"></slot>
                  </template>
                </el-input>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div>
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue'
import { IFormItem, IColLayout } from '@/base-ui/form'
import type { FormInstance } from 'element-plus'

export default defineComponent({
  props: {
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    colLayout: {
      type: Object as PropType<IColLayout>,
      default: () => ({ xl: 6, lg: 8, md: 12, sm: 24, xs: 24, span: 24 })
    },
    itemStyle: {
      type: Object,
      default: () => {
        return { padding: '10px 10px' }
      }
    },
    formStyle: {
      type: Object,
      default: () => {
        return { padding: '22px 0px 0px 0px' }
      }
    },
    modelValue: {
      type: Object,
      required: true
    }
  },
  emits: ['update:modelValue'],

  setup(props, { emit }) {
    let ruleFormRef = ref<FormInstance>()
    const handleValueChange = (value: any, field: string) => {
      emit('update:modelValue', { ...props.modelValue, [field]: value })
    }
    return {
      handleValueChange,
      ruleFormRef
    }
  }
})
</script>

<style scoped lang="less">
.crv-form {
  .header {
    text-align: center;
  }
}
</style>
